package com.ctsi.ssdc.service.impl;


import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import com.ctsi.ssdc.domain.LitemallTopic;
import com.ctsi.ssdc.domain.LitemallTopicExample;
import com.ctsi.ssdc.service.LitemallTopicService;
import com.ctsi.ssdc.repository.LitemallTopicRepository;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
import java.util.Objects;
import com.ctsi.ssdc.service.impl.StrengthenBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.ctsi.ssdc.model.PageResult;
import org.springframework.data.domain.Pageable;
import com.github.pagehelper.PageHelper;
/**
 * Service Implementation for managing LitemallTopic.
 *
 * @author ctsi-biyi-generator
 *
 */
@Service
public class LitemallTopicServiceImpl 
	extends StrengthenBaseServiceImpl<LitemallTopicRepository, LitemallTopic, Integer, LitemallTopicExample> 
	implements LitemallTopicService {

    @Autowired
    LitemallTopicRepository litemallTopicRepository;
    private LitemallTopic.Column[] columns = new LitemallTopic.Column[]{LitemallTopic.Column.id, LitemallTopic.Column.title, LitemallTopic.Column.subtitle, LitemallTopic.Column.price, LitemallTopic.Column.picUrl, LitemallTopic.Column.readCount};


    /**
    * GET  /litemallTopics : get the litemallTopics firstStringBaseColumn.
    */
    @Override
    public PageResult<LitemallTopic> findFirstStringColumn(String title,Pageable pageable){
        String str = title;
        if (Objects.nonNull(pageable)) {
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        }
        LitemallTopicExample litemallTopicExample = new LitemallTopicExample();
        String orderBy = getPageOrderBy(pageable);
        if(StringUtils.isNotEmpty(orderBy)) {
            litemallTopicExample.setOrderByClause(orderBy);
        }
        if (StringUtils.isEmpty(str)){
            litemallTopicExample.createCriteria();
        } else{
            litemallTopicExample.createCriteria().andTitleLike("%" + str +"%");
        }
        List<LitemallTopic>  data = litemallTopicRepository.selectByExample(litemallTopicExample);

        long count = 0L;
        if (CollectionUtils.isNotEmpty(data)){
            count = litemallTopicRepository.countByExample(litemallTopicExample);
        }
        return new PageResult<LitemallTopic>(data,count,count);
    }

    @Override
    public LitemallTopic findById(Integer valueId) {
        LitemallTopicExample example = new LitemallTopicExample();
        example.or().andIdEqualTo(valueId).andDeletedEqualTo(false);
        return litemallTopicRepository.selectOneByExampleWithBLOBs(example);
    }

    @Override
    public List<LitemallTopic> queryList(int offset, int limit) {
        return queryList(offset, limit, "add_time", "desc");
    }

    public List<LitemallTopic> queryList(int offset, int limit, String sort, String order) {
        LitemallTopicExample example = new LitemallTopicExample();
        example.or().andDeletedEqualTo(false);
        example.setOrderByClause(sort + " " + order);
        PageHelper.startPage(offset, limit);
        return litemallTopicRepository.selectByExampleSelective(example, columns);
    }

    @Override
    public List<LitemallTopic> queryRelatedList(Integer id, int offset, int limit) {
        LitemallTopicExample example = new LitemallTopicExample();
        example.or().andIdEqualTo(id).andDeletedEqualTo(false);
        List<LitemallTopic> topics = litemallTopicRepository.selectByExample(example);
        if (topics.size() == 0) {
            return queryList(offset, limit, "add_time", "desc");
        }
        LitemallTopic topic = topics.get(0);

        example = new LitemallTopicExample();
        example.or().andIdNotEqualTo(topic.getId()).andDeletedEqualTo(false);
        PageHelper.startPage(offset, limit);
        List<LitemallTopic> relateds = litemallTopicRepository.selectByExampleWithBLOBs(example);
        if (relateds.size() != 0) {
            return relateds;
        }

        return queryList(offset, limit, "add_time", "desc");
    }

    private String getPageOrderBy(Pageable page) {
        if(page!= null && page.getSort() != null) {
            StringBuilder sb = new StringBuilder();
            page.getSort().forEach(sort -> sb.append(sort.getProperty())
            .append(" ").append(sort.getDirection()).append(","));
            if(sb.length() > 1) {
                return (sb.substring(0,sb.length()-1));
             }
        }
        return null;
    }
}
